package com.suixun.service.credits.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.suixun.dao.JfHideEntranceMapper;
import com.suixun.entity.JfHideEntrance;
import com.suixun.entity.JfHideEntranceExample;
import com.suixun.entity.SysOrgnization;
import com.suixun.enums.credits.UserRange;
import com.suixun.exception.OpServiceException;
import com.suixun.service.RegionService;
import com.suixun.service.credits.HideEntranceService;

import rop.utils.spring.CollectionUtils;

@Service
public class HideEntranceServiceImpl implements HideEntranceService {

	@Resource
	private RegionService regionService;
	@Resource
	private JfHideEntranceMapper jfHideEntranceMapper;
	@Resource
	private SqlSessionTemplate sqlSessionTemplate;

	@Override
	// 存在未加事务的风险
	public void hideEntrance(String uuid, Integer range) {

		if (StringUtils.isBlank(uuid)) {
			throw new OpServiceException("地区不能为空");
		}

		if (range == null) {
			range = 1;
		}

		// 查询uuid是否存在
		SysOrgnization sysOrgnization = regionService.getRegion(uuid);
		if (sysOrgnization == null) {
			throw new OpServiceException("地区不存在");
		}

		// 查询该地区是否已隐藏
		JfHideEntrance jfHideEntrance = getJfHideEntrance(uuid, range);
		if (jfHideEntrance != null) {
			throw new OpServiceException("该地区已隐藏");
		}

		// 隐藏该地区以及该地区下的辖区县地区
		Map<String, Object> map = new HashMap<>();
		map.put("range", range);
		map.put("uuid", uuid);
		sqlSessionTemplate.insert("HideEntranceExtendSql.addhideEntrance", map);
	}

	/**
	 * <p>
	 * Title: addJfHideEntrances
	 * </p>
	 * <p>
	 * 功能描述: 批量新增隐藏区域及该区域下的所有辖区县地区
	 * </p>
	 * 
	 * @param areaId
	 * @param range
	 */
	private void addJfHideEntrances(String areaId, Integer range) {
		String uuids = regionService.getChildRegionIds(areaId);
		List<String> uuidList = getUuidList(uuids);
		if(CollectionUtils.isEmpty(uuidList)) {
			return ;
		}
		Map<String, Object> map = new HashMap<>();
		map.put("range", range);
		map.put("uuid", areaId);
		map.put("uuidList", uuidList);
		sqlSessionTemplate.insert("HideEntranceExtendSql.addhideEntrances", map);
	}
	
	private List<String> getUuidList(String uuids) {
		if(StringUtils.isBlank(uuids)) {
			return null;
		}
		String[] uuidArray = uuids.split(",");
		List<String> uuidList = new ArrayList<>();
		for(String uuid : uuidArray) {
			uuidList.add(uuid);
		}
		return uuidList;
	}

	/**
	 * <p>
	 * Title: deleteJfHideEntrances
	 * </p>
	 * <p>
	 * 功能描述: 批量删除该地区及子地区的隐藏配置
	 * </p>
	 * 
	 * @param areaId
	 * @param range
	 */
	private void deleteJfHideEntrances(String areaId, Integer range) {
		String uuids = regionService.getChildRegionIds(areaId);
		List<String> uuidList = getUuidList(uuids);
		if(CollectionUtils.isEmpty(uuidList)) {
			return ;
		}
		Map<String, Object> map = new HashMap<>();
		map.put("range", range);
		map.put("uuid", areaId);
		map.put("uuidList", uuidList);
		sqlSessionTemplate.delete("HideEntranceExtendSql.deleteHideEntrances", map);
	}

	private JfHideEntrance getJfHideEntrance(String areaId, Integer range) {
		JfHideEntranceExample jfHideEntranceExample = new JfHideEntranceExample();
		jfHideEntranceExample.createCriteria().andAreaIdEqualTo(areaId).andHideRangeEqualTo(range.shortValue());
		List<JfHideEntrance> jfHideEntranceList = jfHideEntranceMapper.selectByExample(jfHideEntranceExample);
		if (CollectionUtils.isEmpty(jfHideEntranceList)) {
			return null;
		} else {
			return jfHideEntranceList.get(0);
		}
	}

	@Override
	public List<JfHideEntrance> getHideEntrances(String uuid) {
		JfHideEntranceExample jfHideEntranceExample = new JfHideEntranceExample();
		jfHideEntranceExample.createCriteria().andAreaIdEqualTo(uuid);
		return jfHideEntranceMapper.selectByExample(jfHideEntranceExample);
	}

	@Override
	public void hideEntrance(String uuid, Boolean guardianHide, Boolean teacherHide) {
		if (StringUtils.isBlank(uuid)) {
			throw new OpServiceException("地区不能为空");
		}

		// 查询uuid是否存在
		SysOrgnization sysOrgnization = regionService.getRegion(uuid);
		if (sysOrgnization == null) {
			throw new OpServiceException("地区不存在");
		}
		
		if(guardianHide == null || teacherHide == null) {
			throw new OpServiceException("积分入口状态不能为空");
		}
		
		// 配置家长端
		hideEntrance(uuid, UserRange.PATRIARCH.getValue(), guardianHide);
		// 配置教师端
		hideEntrance(uuid, UserRange.TEACHER.getValue(), teacherHide);
	}

	private void hideEntrance(String uuid, Integer range, boolean hide) {
		if (hide) {
			// 先删除该地区及子地区的隐藏配置
			deleteJfHideEntrances(uuid, range);
			// 新增该地区及子地区的隐藏配置
			addJfHideEntrances(uuid, range);
		} else {
			// 删除该地区及子地区的隐藏配置
			deleteJfHideEntrances(uuid, range);
		}
	}

}
